#ifndef __M_UTIL_H__
#define __M_UTIL_H__
/*实用工具类的实现
1.获取系统时间
2.判断文件是否存在
3.获取文件所在路径
4.创建目录
*/
#include <iostream>
#include <ctime>
#include <sys/stat.h>

namespace yuanlog
{
    namespace util
    {
        class Date
        {
        public:
            static size_t Now()
            {
                return (size_t)time(nullptr);
            }
        };
        class File
        {
        public:
            static bool exists(const std::string &pathname)
            {
                struct stat st;
                if (stat(pathname.c_str(), &st) < 0)
                {
                    return false;
                }
                return true;
            }
            // 获取文件的所在路径
            static std::string path(const std::string &pathname)
            {
                // ./abc/a.txt
                size_t pos = pathname.find_last_of("/\\");
                if (pos == std::string::npos)
                    return ".";
                else
                    return pathname.substr(0, pos + 1);
            }
            static void createDirectory(const std::string &pathname)
            {
                // ./abc/bcd/cde
                size_t pos = 0, index = 0; // pos 查找“/”的位置
                while (index < pathname.size())
                {
                    pos = pathname.find_first_of("/\\", index);
                    if (pos == std::string::npos)
                    {
                        mkdir(pathname.c_str(), 0777);
                    }
                    std::string parent_dir = pathname.substr(0, pos + 1);

                    if (exists(parent_dir) == true)
                    {
                        index = pos + 1;
                        continue;
                    }
                    mkdir(parent_dir.c_str(), 0777);
                    index = pos + 1;
                }
            }
        };
    };
};

#endif